<?php // $Id: status.php 11 2008-07-30 15:46:18Z the.sk89q $

/**
 * status.php - status icon
 *
 * @version: $Id: status.php 11 2008-07-30 15:46:18Z the.sk89q $
 * @license http://www.gnu.org/copyleft/gpl.html
 * @package dpmatchreserve
 * 
 */

require_once "./common.php";
require_once "include/DigitalPaintServer.php";

// ===============================
// Get input
// ===============================

$server = $_GET['server'];

$server_info = @$CONFIG['servers'][$server];

if (!isset($CONFIG['servers'][$server])) {
    echo "No server specified.";
    exit();
}

// ===============================
// Get status
// ===============================

try {
    $paintball = new DigitalPaintServer($server_info['host'], $server_info['port'], $server_info['rcon_password']);

    if (!$paintball->is_online()) {
        $status = "offline";
    } else if (count($paintball->get_players()) == 0) {
        $status = "available";
        
        if ($CONFIG['grace_period'] > 0) {
            $last_lease = @filemtime("cache/last_lease_$server");
            
            if ($last_lease > time()-$CONFIG['grace_period']) {
                $status = "reserved";
            }
        }
    } else {
        $status = "reserved";
    }
} catch (DPSConnectionException $e) {
    $status = "offline";
}

// ===============================
// Send it off!
// ===============================

header("Content-Type: image/png");
readfile("themes/{$CONFIG['theme']}/images/status/{$status}.png");
